package com.hzhr.iot.core.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class HzhrJsonUtil {
    /**
     * 转换json对象类型
     * @param in
     * @param outClass
     * @param <T>
     * @return
     */
    public static <T> T convertObjectType(Object in, Class<T> outClass) {
        if (in == null) return null;
        String jsonStr = JSON.toJSONString(in);
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        T result = jsonObject.toJavaObject(outClass);
        return result;
    }

    /**
     * 对象转json字符串
     * @param in
     * @return
     */
    public static String object2json(Object in) {
        if (in == null) return null;
        String jsonStr = JSON.toJSONString(in);
        return jsonStr;
    }

    /**
     * json字符串转对象
     * @param jsonStr
     * @param outClass
     * @param <T>
     * @return
     */
    public static <T> T json2object(String jsonStr, Class<T> outClass) {
        if (jsonStr == null) return null;
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        T result = jsonObject.toJavaObject(outClass);
        return result;
    }
}
